package category

import (
	"goskeleton/app/model"
)

type ArticleModel struct {
	model.BaseModel
	ParentId int    `gorm:"column:parent_id" json:"parent_id"`
	Name     string `json:"name"`
}

func CreateArticleFactory(sqlType string) *ArticleModel {
	return &ArticleModel{BaseModel: model.BaseModel{DB: model.UseDbConn(sqlType)}}
}

//表名
func (a *ArticleModel) TableName() string {
	return "tb_category_article"
}

func (a *ArticleModel) Create(parent_id float64, name string) bool {
	sql := "INSERT  INTO tb_category_article(parent_id,name)  values(?,?)"
	result := a.Exec(sql, parent_id, name)
	if result.RowsAffected > 0 {
		return true
	} else {
		return false
	}
}

func (a *ArticleModel) List() (list []ArticleModel) {
	sql := "SELECT id,parent_id,name,DATE_FORMAT(created_at,'%Y-%m-%d %H:%i:%s')  AS created_at,	DATE_FORMAT(updated_at,'%Y-%m-%d %H:%i:%s')  AS updated_at  FROM  tb_category_article "
	if res := a.Raw(sql).Find(&list); res.RowsAffected > 0 {
		return list
	} else {
		return nil
	}
}
